package com.c.yinyuezhushou.Dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.c.yinyuezhushou.Entity.SongListEntity;
import com.c.yinyuezhushou.Fragment.MineFragment;
import com.c.yinyuezhushou.Interface.MyInterface;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.R;
import com.c.yinyuezhushou.Service.DownService;
import com.c.yinyuezhushou.Service.MusicService;
import com.c.yinyuezhushou.Sql.MyDButil;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import java.util.List;

public class SongListBoDialog extends BottomSheetDialogFragment {
    private SongListEntity songListEntity;
    private MineFragment mineFragment;
    private List<MusicItem> list;

    public SongListBoDialog(SongListEntity songListEntity,MineFragment mineFragment) {
        this.songListEntity = songListEntity;
        this.mineFragment = mineFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_song_listbo,container,false);
        MyApplication myApplication = (MyApplication) getActivity().getApplicationContext();
        MyDButil myDButil = myApplication.getMyDButil();
        list = myDButil.getSongListMusicItem(songListEntity.getListname());

        ImageView imageView = view.findViewById(R.id.dslb_image1);
        TextView textView = view.findViewById(R.id.dslb_text1);

        ImageView imageView2 = view.findViewById(R.id.dslb_image2);
        TextView textView2  = view.findViewById(R.id.dslb_text2);

        ImageView imageView3 = view.findViewById(R.id.dslb_image3);
        TextView textView3  = view.findViewById(R.id.dslb_text3);

        ImageView imageView4 = view.findViewById(R.id.dslb_image4);
        TextView textView4  = view.findViewById(R.id.dslb_text4);

        if (songListEntity.getImageurl().equals("")){
            Glide.with(getContext()).load(R.drawable.icon_songlist).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(imageView);
        }else{
            Glide.with(getContext()).load(songListEntity.getImageurl()).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(imageView);
        }
        textView.setText(songListEntity.getListname());

        View.OnClickListener clickListener1 = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MusicService musicService = myApplication.getMusicService();
                if (list.size()!=0){
                    musicService.setMusicItems(list);
                    musicService.prepareToPlay(list.get(0));
                }else {
                    Toast.makeText(myApplication, "歌单无歌曲！", Toast.LENGTH_SHORT).show();
                }
                
            }
        };

        View.OnClickListener clickListener2 = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (list.size()!=0){
                    DownService downService = myApplication.getDownService();
                    for (MusicItem musicItem : list){
                        downService.downloadMusicItem(musicItem,myApplication.getSetup().getDownbr());
                    }
                }else {
                    Toast.makeText(myApplication, "歌单无歌曲！", Toast.LENGTH_SHORT).show();
                }

            }
        };

        View.OnClickListener clickListener3 = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyDialog myDialog = new MyDialog(getContext(), true, null, "是否删除该歌单？","", new MyInterface() {
                    @Override
                    public void con() {
                        mineFragment.removeSongList(songListEntity.getListname());
                    }

                    @Override
                    public void can() {

                    }

                    @Override
                    public void musicItem(MusicItem musicItem) {

                    }
                });
                myDialog.show();
            }
        };

        imageView2.setOnClickListener(clickListener1);
        textView2.setOnClickListener(clickListener1);
        imageView3.setOnClickListener(clickListener2);
        textView3.setOnClickListener(clickListener2);
        imageView4.setOnClickListener(clickListener3);
        textView4.setOnClickListener(clickListener3);

        return view;
    }
}
